<template>
  <div class="page-container">
    <vxe-grid ref="tableRef" v-bind="gridOptions" @sort-change="sort">
      <template #handle="{ row }">
        <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:vendorConsumerLedger:query']">详情</el-button>
        <!-- <el-button link type="primary" @click="handleShipping(row)" v-hasPermi="['biz:vendorConsumerLedger:add']">发货</el-button> -->
        <el-button link type="danger" @click="handleDelivering(row)" v-hasPermi="['biz:vendorConsumerLedger:receive']">收货</el-button>
      </template>
      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
    <oper-add ref="operAddRef" @success="handleQuery"></oper-add>
  </div>
</template>

<script setup lang="ts">
import { listUserLedger } from '@/api/vendor/ledger/index';
import { BizVendorLedgerVo } from '@/api/vendor/ledger/types';
import { getTableConfig } from './columns';
import OperAdd from '@/views/vendor/ledger/common/oper-add/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const operAddRef = ref();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  name: '',
  regionCode: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUserLedger(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/**
 * 排序
 * @param field
 * @param order
 */
const sort = async (field, order) => {
  let fieldName = field.field;
  if (fieldName == 'vendorName') fieldName = 'vendorId';

  queryParams.value.orderByColumn = fieldName;
  queryParams.value.isAsc = field.order;

  getList();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 发货 */
const handleShipping = (row?: BizVendorLedgerVo) => {
  operAddRef.value.open('1', row?.vendorId, row?.vendorName);
};
/** 收货 */
const handleDelivering = async (row?: BizVendorLedgerVo) => {
  operAddRef.value.open('2', row?.vendorId, row?.vendorName);
};
/** 详情按钮操作 */
const handleDetail = async (row?: BizVendorLedgerVo) => {
  proxy?.$tab.openPage('/vendorInfo/vendorLedger/info', '', { id: row?.vendorId });
};

onMounted(() => {
  getList();
});
</script>
